package coms.process;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import org.json.*;

import com.beans.AdminUserBean;
import com.crc.mam.AESUtil;
import com.crc.mam.BaseCase;
import com.crc.mam.PropertiesUtil;
import TestCases.BaseTestCase;
import coms.AutoException;
import coms.Constants;
import frame.FrameWork;
import frame.HttpClientHelper;
import frame.HttpHelper;
import frame.InterTest;
import jdk.nashorn.internal.parser.JSONParser;
import sun.util.locale.provider.LocaleServiceProviderPool.LocalizedObjectGetter;

/**
 * 登录流程
 * @author CHENHAILIAN6
 *
 */
public class LoginTest extends BaseCase implements InterTest {
	private static Logger logger = LoggerFactory.getLogger(LoginTest.class);
	private String userCode;
	private String password;
	private String captcha;
    private String url;
    String crcToken;
    AdminUserBean loginUserBean;
   
    public String getCrcToken() {
		return crcToken;
	}

	public void setCrcToken(String crcToken) {
		this.crcToken = crcToken;
	}



    public AdminUserBean getLoginUserBean() {
		return loginUserBean;
	}

	public void setLoginUserBean(AdminUserBean loginUserBean) {
		this.loginUserBean = loginUserBean;
	}

	

	

	public LoginTest() {
    	/*String ddd = "{ \"userCode\":\"wx\",\"password\":\"123456\",\"captcha\":\"123\"}";*/
    	
    }
	
	public LoginTest(String userCode,String password) {
		this.userCode = userCode;
    	this.password = password;
    	this.captcha = "1";
	}
    
    @Override
	public String toString() {
		return "LoginTest [userCode=" + userCode + ", password=" + password + ", captcha=" + captcha + ", url=" + url
				+ "]";
	}
    



	@Override
	public void run(Map<Object, Object> paramMap) {
	  this.url =Constants.LOGIN_URL;
	  String baseUrl = (String) paramMap.get("baseUrl");
      if(baseUrl !=null) {
        FrameWork.setBaseUrl(baseUrl);
      }

		String userCode1 = (String) paramMap.get("userCode");
		String password1 = (String) paramMap.get("password");
		if(userCode1 != null){ 
		  this.userCode = userCode1;
		  }else {
		    this.userCode = Constants.USER_CODE;
		  }
		if(password1 != null){ 
		  this.password = password1;
		  }else {
		    this.password = Constants.PASSWORD;
		  }
		this.captcha = "1";
		String datas = "{\"userCode\":\""+userCode+"\",\"password\":\""+password+"\",\"captcha\":\""+captcha+"\"}";
		logger.info("logindatas params---" + datas);
		try {
			
			String encryDatas = AESUtil.encrypt(datas);
			System.out.println("加密后："+encryDatas);
			logger.info("加密的编码----"+encryDatas);
			String jsonObject = "{params:\"" + encryDatas+"\"}";
			/*result = HttpHelper.post("C3F06A0B7BBFE7043239264576A1B6F5",jsonObject, url);*/
			result = HttpHelper.post(jsonObject, url);
			
			logger.info("login result ---" + result);
			JSONObject jsonObj = new JSONObject(result);
			String code = jsonObj.getString("statusCode").trim();
			//System.out.println(code);
			if(code.equals("0")) {
			  status = true;
				
			}else {
			  status = false;
			  remark = jsonObj.getString("msg");
			}
		//	adminUser();
		}catch(Exception e) {
		  e.printStackTrace();
		  status = false;
		  remark = "系统异常";
		}
		
	}

	
public void adminUser() {
	String url = Constants.ADMIN_URL;
	try {
		result = HttpHelper.post("", url);
		logger.info("adminUser 获取用户信息 --- >" + result);
		try {
		JSONObject jsonObj = new JSONObject(result);
		
		String occupation = "";
		try{
			occupation = (String)jsonObj.getString("occupation");
		 }catch(JSONException e) {
			 e.printStackTrace();
			 
	     }
		String companyDesc = "";
		try{
			companyDesc = (String)jsonObj.getString("companyDesc");
		 }catch(JSONException e) {
			 e.printStackTrace();
			 
	     }
		String departmentId = "";
		try{
			departmentId = (String)jsonObj.getString("departmentId");
		 }catch(JSONException e) {
			 e.printStackTrace();
			 
	     }
		String telephone = "";
		try{
			telephone = (String)jsonObj.getString("telephone");
		 }catch(JSONException e) {
			 e.printStackTrace();
			 
	     }
		String empNo = "";
		try{
			empNo = (String)jsonObj.get("empNo");
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }
		String userName = "";
		try{
			userName = (String)jsonObj.get("userName");
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }
		String substitute = "";
		try{
			substitute = String.valueOf(jsonObj.get("substitute"));
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }
		String userCode = "";
		try{
			userCode = (String)jsonObj.get("userCode");
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }
		String occupationCode = "";
		/*try{
			occupationCode = (String)jsonObj.get("occupationCode");
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }*/
		String company = "";
		try{
			company = (String)jsonObj.get("company");
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }
		String applyDate = "";
		try{
			applyDate = (String)jsonObj.get("applyDate");
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }
		String department = "";
		try{
			department = (String)jsonObj.get("department");
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }
		String email = "";
		try{
			email = (String)jsonObj.get("email");
		 }catch(Exception e) {
			 e.printStackTrace();
			 
	     }
		loginUserBean = new AdminUserBean(occupation, companyDesc,  departmentId,  telephone,  empNo,
				 userName,  substitute,  userCode,  occupationCode,  company,
				 applyDate,  department,  email,null,null,null,null);
		logger.info("当前用户信息-- " + loginUserBean.toString());
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}catch(Exception e) {
		e.printStackTrace();
	}
}

@Override
public boolean getStatus() {
  // TODO Auto-generated method stub
  return status;
}
	

@Override
public String getRemark() {
  return remark;
}
	
}
